package com.epfresh.supplier.activity.test_report;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.api.dialog.TimePickerDialog;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.BookGoodsMonthExpandListAdapter;
import com.epfresh.supplier.adapter.TestReportExpandListAdapter;
import com.epfresh.supplier.entity.BookGoodsMonth;
import com.epfresh.supplier.entity.TestReport;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TestReportActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG="TestReportActivity";
    private RelativeLayout rl_no_data;
    private ExpandableListView lv_deal_detail;
    private TestReportExpandListAdapter bookYearGoodsAdapter;
    private ArrayList<TestReport.SupplyOrderIncomeDayListBean> list = new ArrayList<>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_report);
        tvBack.setVisibility(View.VISIBLE);
        ivBack.setVisibility(View.VISIBLE);
        righticon.setVisibility(View.VISIBLE);
        righticon.setBackgroundResource(R.mipmap.calendar);
        righticon.setOnClickListener(this);
        toolbarTitle.setText("检测报告");

        rl_no_data =findViewById(R.id.rl_no_data);
        lv_deal_detail =findViewById(R.id.lv_deal_detail);

        bookYearGoodsAdapter = new TestReportExpandListAdapter(this,lv_deal_detail,list);
        //bookYearGoodsAdapter.setOnItemClickListener(this);
        lv_deal_detail.setAdapter(bookYearGoodsAdapter);
        //设置组点击事件
        lv_deal_detail.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {
                return true;
            }
        });

        //加载数据列表
//        requestTestReportList("");
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.righticon://选择日期
                SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String date = sDateFormat.format(new Date());
                openDate(date);
                break;
        }
    }


    private void openDate(String time) {
        TimePickerDialog timeDialog = new TimePickerDialog(this);




        timeDialog.setOnPositiveClickListener(new TimePickerDialog.OnPositiveClickListener() {
            @Override
            public void onPositiveClickListener(int index, String data) {
                Log.e(TAG,"选择的时间:"+data);
//                requestTestReportList(data);
            }
        });


        timeDialog.setTitle("选择日期");
        timeDialog.setCurrentDate(time);

        timeDialog.show();
    }



    /**
     * 加载列表数据
     */
    private void requestTestReportList(String chooseDate) {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductIncomeForDayOfMonth);
        Map<String, String> map = new HashMap<>();
        map.put("storeId", BaseApplication.getInstance().getStoreId());

        map.put("accountId",getUser().getAccountId());
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductIncomeForDayOfMonth, onRequestListener);
    }

    private OnRequestListener<TestReport> onRequestListener = new OnRequestListener<TestReport>() {
        @Override
        public TestReport jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, TestReport.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<TestReport> response, Object tag) {
            //隐藏加载动画
            hideProgressDialog();
            //获取数据
            TestReport responseElement = response.getResponseElement();

            list.clear();
            list.addAll(responseElement.getSupplyOrderIncomeDayList());
            bookYearGoodsAdapter.notifyDataSetChanged();

            //判断是否有数据
            if (list.size()>0){
                rl_no_data.setVisibility(View.GONE);
            }else {
                rl_no_data.setVisibility(View.VISIBLE);
            }
        }
    };

}
